<?php

namespace app\admin\controller\order;

use app\common\controller\Backend;
use think\Db;

/**
 * 任务管理
 *
 * @icon fa fa-circle-o
 */
class Room extends Backend
{

    /**
     * Task模型对象
     * @var \app\common\model\shop\Task
     */
    protected $model = null;

    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'take_key_id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new  \app\common\model\shop\task\Key;

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            //$_where["flag"] = 2;//插旗子订单
            $join[] = ["sd_shop_task s","s.task_id = k.task_id","left"];

            $field = "s.task_sno,s.goods_sno,s.service_change_price,s.beat_price";//任务表

            $field .= ",k.key_word,k.order_amount,k.cancle_count,k.left_num,k.left_task_num_one,k.left_task_num_tow";//任务字段表

            $total = $this->model
                ->alias("k")
                ->join($join)
                ->where($where)
                //->where($_where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->alias("k")
                ->join($join)
//                ->with("task")
//                ->field('*,take_key_id as id')
                ->where($where)
                //->where($_where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }




    /**
     *
     * 编辑
     *
     */
    public function edit($ids = NULL){

        //halt(input());
        $row = $this->model
            ->with('task')
            ->field('*,take_key_id as id')
            ->where("take_key_id",$ids)
            ->find();
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params) {
                $shop_task_key_info = Db::name("shop_task_key")->where("take_key_id",$params["take_key_id"])->field("left_num")->find();

                //房间1所有订单数
                $left_task_num_one = intval($params["left_task_num_one"]);

                //房间2所有订单数
                $left_task_num_tow = intval($params["left_task_num_tow"]);


                if(($left_task_num_one+$left_task_num_tow)>$shop_task_key_info["left_num"]){
                    renderJson("设置房间数量有误",0);
                }

                try{

                    $op = $this->model->save(["left_task_num_one"=>$left_task_num_one,"left_task_num_tow"=>$left_task_num_tow],["take_key_id"=>$params["take_key_id"]]);

                    if($op){

                        renderJson("设置房间成功",1);

                    }else{

                        renderJson("设置房间有误",0);
                    }

                }catch (\Exception $e){

                    renderJson($e->getMessage(),0);

                }
            }

        }
        //halt($row);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


}